//
//  ViewController.m
//  UIGextureRecognizer
//
//  Created by Hanlingyu on 16/7/18.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, assign) BOOL changeImage;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor redColor];
    
    // 手势识别器 (UIGestureRecognizer)
    //
    // 分为7种
    // 1. 轻拍
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 点击几次
    tap.numberOfTapsRequired = 2;
    // 几个手指 alt / option
    tap.numberOfTouchesRequired = 2;
    [self.view addGestureRecognizer:tap];
    
    
    
    // 2. 长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    // 长按几秒后开始, 默认0.5秒
    longPress.minimumPressDuration = 1;
    // 默认10像素, 长按失败前最大的移动距离
    longPress.allowableMovement = 1000;
    [self.view addGestureRecognizer:longPress];
    
    // 3. 清扫
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    // direction可以设置多个, 前提, 多个方向所触发的行为要相同
    // 如果多个方向所触发的行为不同, 需要创建多个轻扫的手势
    // 左右 与 上下 有冲突
    swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
//    [self.view addGestureRecognizer:swipe];
    
    
    // 练习: ImageView添加清扫手势
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    imageView.backgroundColor = [UIColor grayColor];
    imageView.image = [UIImage imageNamed: @"01.jpg"];
    [self.view addSubview:imageView];
    
//    UISwipeGestureRecognizer *lightSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(lightSwipeRightChangeImage:)];
////    将图片的交互打开
    imageView.userInteractionEnabled = YES;
//    [imageView addGestureRecognizer:lightSwipeRight];
//    
//    UISwipeGestureRecognizer *lightSwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(lightSwipeLeftChangeImage:)];
//    //    将图片的交互打开
//    lightSwipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
//    imageView.userInteractionEnabled = YES;
//    [imageView addGestureRecognizer:lightSwipeLeft];
//    
//    
//    UISwipeGestureRecognizer *changImageSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeImage:)];
//    // 按位枚举, 4个选项 -> 1 1 1 1
//    // 全选的情况 1(3) 1(2) 1(1) 1(0)
////    changImageSwipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
//    changImageSwipe.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
//    [imageView addGestureRecognizer:changImageSwipe];
//    self.changeImage = YES;
    
    // 4. 平移(pan)
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [imageView addGestureRecognizer:pan];
    
    
    // 5. 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [imageView addGestureRecognizer:pinch];

    
    
    
    
    
    // 6. 旋转
    
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [imageView addGestureRecognizer:rotation];
    
    // 7. 屏幕边缘手势
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanAction:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    
    
}

- (void)screenEdgePanAction:(UIScreenEdgePanGestureRecognizer *)screenEdgePan {
    NSLog(@"屏幕边缘手势");
}



- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {

    // 转换中, 根据上次转换继续改变
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    // 旋转的弧度为0
    rotation.rotation = 0;

}




- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {


    NSLog(@"%f", pinch.scale);
    
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    // 设置放大倍数
    pinch.scale = 1;


}


- (void)panAction:(UIPanGestureRecognizer *)pan {
    
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        // 平移开始
    } else if (pan.state == UIGestureRecognizerStateChanged) {
        // 平移中
        // 平移收拾, 根据其他View上移动的过程, 得到最终点
        CGPoint point = [pan translationInView:pan.view];
        //    NSLog(@"%@", NSStringFromCGPoint(point));
        // 根据point使pan.view移动
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        // pan 移动的点置空
        [pan setTranslation:CGPointZero inView:pan.view];
    } else {
        // 平移结束, 回到初始位置
        pan.view.transform = CGAffineTransformIdentity;
    }
}

- (void)changeImage:(UISwipeGestureRecognizer *)swipe {

    
    UIImageView *imageView = (UIImageView *)swipe.view;
    if (swipe.direction & UISwipeGestureRecognizerDirectionDown ) {
        NSLog(@"1111");
        
    }
    if (swipe.direction & UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"2222");
    }

//    
//    if (self.changeImage == YES) {
//        imageView.image = [UIImage imageNamed:@"userHeaderImage.jpg"];
//        self.changeImage = NO;
//    } else {
//        imageView.image = [UIImage imageNamed:@"01.jpg"];
//        self.changeImage = YES;
//    }
    


}

- (void)lightSwipeRightChangeImage:(UISwipeGestureRecognizer *)lightSwipeRight {

    NSLog(@"轻扫");
    UIImageView *imageView = (UIImageView *)lightSwipeRight.view;
    imageView.image = [UIImage imageNamed:@"userHeaderImage.jpg"];
    
}
- (void)lightSwipeLeftChangeImage:(UISwipeGestureRecognizer *)lightSwipeLeft {
    
    NSLog(@"轻扫");
    UIImageView *imageView = (UIImageView *)lightSwipeLeft.view;
    imageView.image = [UIImage imageNamed:@"01.jpg"];
    
}



- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"轻拍");
}

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    
    // 手势有状态, 是枚举类型, 是父类(UIGestureRecognizer)的属性
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    } else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束");
    }
}

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {
    NSLog(@"轻扫");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
